import 'dart:developer';

import '../index.dart';
import 'package:intl/intl.dart';
import 'dart:convert';

class TaskLogItem extends StatefulWidget {
  TaskLogItem(
      {Key? key, required this.taskLog, this.itemLocation = ItemLocation.other})
      : super(key: key);

  late TaskLog taskLog;
  late ItemLocation itemLocation;

  @override
  _TaskLogItemState createState() => _TaskLogItemState();
}

class _TaskLogItemState extends State<TaskLogItem> {
  @override
  Widget build(BuildContext context) {
    List opertionContentList =
    jsonDecode(widget.taskLog.opertionContent ?? "");

    List<Widget> opertionContentColumnList = [];
    opertionContentColumnList.add(SizedBox(
      height: 14,
    ));

    opertionContentColumnList.add(Text(widget.taskLog.opertionUserName ?? "",
        style: Theme.of(context).textTheme.titleMedium));
    opertionContentColumnList.add(SizedBox(
      height: 5,
    ));
    opertionContentColumnList.add(Text(
      widget.taskLog.opertionName ?? "",
      style: Theme.of(context)
          .textTheme
          .bodyMedium
          ?.copyWith(color: Theme.of(context).colorScheme.onSecondary),
    ));

    for (Map map in opertionContentList) {
      opertionContentColumnList.add(SizedBox(
        height: 5,
      ));
      String key = map["key"];
      String type = map["type"];
      String value = map["value"];
      if (key.isNotEmpty) {
        if (type == "text") {
          List<Widget> rowList = [];
          rowList.add(Text(
            "${key}：",
            style: Theme.of(context)
                .textTheme
                .bodyMedium
                ?.copyWith(color: Theme.of(context).colorScheme.onSecondary),
          ));
          rowList.add(Expanded(
              child: Text(
            value,
            style: Theme.of(context).textTheme.bodyMedium,
          )));

          opertionContentColumnList.add(Row(
              crossAxisAlignment: CrossAxisAlignment.start, children: rowList));
        } else {
          opertionContentColumnList.add(Text(
            "${key}：",
            style: Theme.of(context)
                .textTheme
                .bodyMedium
                ?.copyWith(color: Theme.of(context).colorScheme.onSecondary),
          ));
          opertionContentColumnList.add(SizedBox(
            height: 5,
          ));
          //添加相册
          opertionContentColumnList.addAll(_getPhotoWidget(value));
        }
      } else {
        if (type == "text") {
          opertionContentColumnList.add(Text(
            value,
            style: Theme.of(context)
                .textTheme
                .bodyMedium
                ?.copyWith(color: Theme.of(context).colorScheme.onSecondary),
          ));
        } else {
          //添加相册
          opertionContentColumnList.addAll(_getPhotoWidget(value));
        }
      }
    }
    opertionContentColumnList.add(SizedBox(
      height: 10,
    ));

    List<Widget> lineColumnList = [];

    if (widget.itemLocation == ItemLocation.head ||
        widget.itemLocation == ItemLocation.only) {
      lineColumnList.add(SizedBox(
        height: 15,
      ));
    } else {
      lineColumnList.add(LinePacking(
        height: 15,
      ));
    }
    lineColumnList.add(CirclePacking());
    if (widget.itemLocation == ItemLocation.head ||
        widget.itemLocation == ItemLocation.other) {
      lineColumnList.add(Expanded(child: LinePacking(height: 0)));
    }

    DateTime time = DateFormat('yyyy-MM-dd HH:mm:ss')
        .parse(widget.taskLog.createTime ?? "");

    return IntrinsicHeight(
        child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(
          width: 100,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              SizedBox(
                height: 15,
              ),
              Text(
                DateFormat('yyyy-MM-dd').format(time),
                style: Theme.of(context)
                    .textTheme
                    .labelMedium
                    ?.copyWith(color: Theme.of(context).colorScheme.onSecondary),
              ),
              SizedBox(
                height: 5,
              ),
              Text(
                DateFormat('HH:mm').format(time),
                style: Theme.of(context)
                    .textTheme
                    .labelMedium
                    ?.copyWith(color: Theme.of(context).colorScheme.onSecondary),
              )
            ],
          ),
        ),
        SizedBox(
          width: 10,
        ),
        Column(
          children: lineColumnList,
        ),
        SizedBox(
          width: 10,
        ),
        Expanded(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: opertionContentColumnList,
        ))
      ],
    ));

    // return ListTile(
    //   title: Text(widget.taskLog.opertionUserName ?? ""),
    //   trailing: Text(widget.taskLog.createTime ?? ""),
    //   subtitle: Text(
    //       "${widget.taskLog.opertionName}\n${widget.taskLog.opertionContent}"),
    // );
  }

  /*每行数量*/
  static const int kRowNum = 3;

  List<Widget> _getPhotoWidget(String photoStr) {
    // /*相片列表*/
    late List<TyPhoto> photos = [];
    var list = photoStr.split(",");
    for (String url in list) {
      if (url.isNotEmpty) {
        photos.add(TyPhoto(url: url));
      }
    }

    int length = photos.length;

    double width = 76.67;
    double margin = 16;
    double allWidth = MediaQuery.of(context).size.width - 160;
    if (kRowNum * width + (kRowNum - 1) * margin < allWidth) {
      width = (allWidth - (kRowNum - 1) * margin) / kRowNum;
    } else {
      margin = (allWidth - kRowNum * width) / (kRowNum - 1);
    }
    int column = (length / kRowNum).ceil();
    List<Widget> columnList = [];
    for (int i = 0; i < column; i++) {
      List<Widget> rowList = [];
      if (i > 0) {
        columnList.add(const SizedBox(
          height: 16,
        ));
      }
      for (int j = 0; j < kRowNum; j++) {
        int index = i * kRowNum + j;
        if (j > 0) {
          rowList.add(SizedBox(
            width: margin,
          ));
        }
        if (index < photos.length) {
          TyPhoto photo = photos[index];
          rowList.add(
            InkWell(
              child: Container(
                width: width,
                height: width,
                decoration: ShapeDecoration(
                    image: DecorationImage(
                        image: NetworkImage(photo.thumbnail()!), fit: BoxFit.cover),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadiusDirectional.circular(3.33))),
              ),
              onTap: () {
                //点击事件
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => PreviewPhotoPage(
                          photos: photos,
                          index: index,
                        )));
              },
            ),
          );
        }
      }
      columnList.add(Row(
        children: rowList,
      ));
    }
    return columnList;
  }
}
